function paging(page){
    $("[name=page]").val(page);
    $("#searchForm").submit();
}

$("div").delegate(".btn-setting","click",function(){
    $("#addResult").click();
    return true;
});
$(function(){
	
	var option_list = "";
	for(var i= 0; i < 10; i++){
		var year = new Date().getFullYear();
		year += i;
		option_list += "<option value='"+year+"'>"+year+"</option>";
	}
	$("#add-startTime").empty().append(option_list);
	$("#add-endTime").empty().append(option_list);
	$("#edit-startTime").empty().append(option_list);
	$("#edit-endTime").empty().append(option_list);
	
	/*$(".hide-btn").click(function(){
		var id = $(this).attr("id");
		var count = 0;
		$.ajax({
            url:"/competitionSeason/checkShowSeason.json?status=2",
            type:'POST',
            async:false,
            dataType:"json",
            success:function(data) {
            	count = data;
            }
        });
		
		$.ajax({
            url:"/competitionSeason/addOrUpdate.json?id="+id+"&status=2",
            type:'POST',
            dataType:"json",
            success:function(date) {
                if(date){
                    $(".close").click();
                    alert("操作成功！");
                    window.location.href = "/competitionSeason/list.htm";
                }else{
                   alert("操作失败！");
                    window.location.href = "/competitionSeason/list.htm";
                }
            }
        });
	});*/
	
	$("#add-startTime").change(function(){
		var startTime = $("#add-startTime").find("option:selected").val();
    	var endTime = $("#add-endTime").find("option:selected").val();
    	if(startTime == endTime){
    		$(".add-month").show();
    	}else{
    		$("#add-startMonth").val("");
    		$("#add-endMonth").val("");
    		$(".add-month").hide();
    	}
	});
	$("#add-endTime").change(function(){
		var startTime = $("#add-startTime").find("option:selected").val();
    	var endTime = $("#add-endTime").find("option:selected").val();
    	if(startTime == endTime){
    		$(".add-month").show();
    	}else{
    		$("#add-startMonth").val("");
    		$("#add-endMonth").val("");
    		$(".add-month").hide();
    	}
	});
	
	$("#edit-startTime").change(function(){
		var startTime = $("#edit-startTime").find("option:selected").val();
    	var endTime = $("#edit-endTime").find("option:selected").val();
    	if(startTime == endTime){
    		$(".edit-month").show();
    	}else{
    		$("#edit-startMonth").val("");
    		$("#edit-endMonth").val("");
    		$(".edit-month").hide();
    	}
	});
	$("#edit-endTime").change(function(){
		var startTime = $("#edit-startTime").find("option:selected").val();
    	var endTime = $("#edit-endTime").find("option:selected").val();
    	if(startTime == endTime){
    		$(".edit-month").show();
    	}else{
    		$("#edit-startMonth").val("");
    		$("#edit-endMonth").val("");
    		$(".edit-month").hide();
    	}
	});
	
	$(".show-btn").click(function(){
		var id = $(this).attr("id");
		$.ajax({
            url:"/competitionSeason/showCompetitionSeason.json?id="+id+"&status=1",
            type:'POST',
            dataType:"json",
            success:function(date) {
                if(date){
                    $(".close").click();
                    alert("操作成功！");
                    window.location.href = "/competitionSeason/list.htm";
                }else{
                   alert("操作失败！");
                    window.location.href = "/competitionSeason/list.htm";
                }
            }
        });
	});
	
	$('.edit-btn').click(function(e){
        e.preventDefault();
        $("#edit-name").val($("#name-"+e.currentTarget.id).val());
        var startTime = $("#startTime-"+e.currentTarget.id).val();
    	var endTime = $("#endTime-"+e.currentTarget.id).val();
        $("#edit-startTime").val(startTime);
        $("#edit-endTime").val(endTime);
        $("#edit-startMonth").val($("#startMonth-"+e.currentTarget.id).val());
        $("#edit-endMonth").val($("#endMonth-"+e.currentTarget.id).val());
    	if(startTime == endTime){
    		$(".edit-month").show();
    	}else{
    		$(".edit-month").hide();
    	}
        
        $("#edit-id").val(e.currentTarget.id);
        $('#editModel').modal('show');
    });
	
	$(".competition-manage-btn").click(function(){
		var seasonId = $(this).attr("id");
		window.location.href = "/competitionTeam/list.htm?seasonId="+seasonId;
	});
	
	$(".score-manage-btn").click(function(){
		var seasonId = $(this).attr("id");
		window.location.href = "/scoreManage/list.htm?seasonId="+seasonId;
	});
	
    $("#updateSeason").validate( {
        checkStart : true,
        errorElement: 'span',
        errorClass: 'help-inline',
        focusInvalid: false,
        rules : {
            name : {
                required: true
            }
        },
        messages : {
            name : {
                required : "必须填写"
            }
        },
        submitHandler : function(form) {
        	var startTime = $("#edit-startTime").find("option:selected").val();
        	var endTime = $("#edit-endTime").find("option:selected").val();
        	if(startTime > endTime){
        		alert("赛季时间有误");
        		return;
        	}else if(startTime == endTime){
        		var startMonth = $("#edit-startMonth").find("option:selected").val();
            	var endMonth = $("#edit-endMonth").find("option:selected").val();
            	if(startMonth == '' || startMonth == null || endMonth == '' || endMonth == null || startMonth > endMonth){
            		alert("赛季月份有误");
            		return;
            	}
        	}
        	
            $("#updateSeason").ajaxSubmit(function(date,res,me){
                if(date){
                    alert("修改成功了！");
                    $(".close").click();
                    window.location.href = "/competitionSeason/list.htm";
                }else{
                	alert("修改失败了！");
                }
            });
        },
        highlight: function (e) {
            $(e).closest('.control-group').removeClass('info').addClass('error');
        }
    });

    //添加
    $("#addSeason").validate( {
        checkStart : true,
        errorElement: 'span',
        errorClass: 'help-inline',
        focusInvalid: false,
        rules : {
            name : {
                required: true
            }
        },
        messages : {
            name : {
                required : "必须填写"
            }
        },
        submitHandler : function(form) {
        	
        	var startTime = $("#add-startTime").find("option:selected").val();
        	var endTime = $("#add-endTime").find("option:selected").val();
        	if(startTime > endTime){
        		alert("赛季时间有误");
        		return;
        	}else if(startTime == endTime){
        		var startMonth = $("#add-startMonth").find("option:selected").val();
            	var endMonth = $("#add-endMonth").find("option:selected").val();
            	if(startMonth == '' || startMonth == null || endMonth == '' || endMonth == null || startMonth > endMonth){
            		alert("赛季月份有误");
            		return;
            	}
        	}
        	
            $("#addSeason").ajaxSubmit(function(date,res,me){
                if(date){
                    alert("新建成功了！");
                    $(".close").click();
                    window.location.href = "/competitionSeason/list.htm";
                }else{
                	alert("新建失败了！");
                }
            });
        },
        highlight: function (e) {
            $(e).closest('.control-group').removeClass('info').addClass('error');
        }
    });    
    
    var options = {
            currentPage: $("[name=page]").val(),
            totalPages: $("[name=totalPage]").val(),
            alignment:"left",
            itemTexts: function (type, page, current) {
                switch (type) {
                    case "first":
                        return "首页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "末页";
                    case "page":
                        return page;
                }
            },
            pageUrl: function(type,page,current){
                return "javascript:paging('"+page+"');";
            }
        };
    $('#example').bootstrapPaginator(options);
     
});